<?php
namespace Home\Model;
use Think\Model;
use Org\Util\Tree;
// +----------------------------------------------------------------------
// | Date:2017年1月3日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class CommentModel extends Model{
 
    /**
     * 获取全部评论，
     * @param number $page
     * @param number $limit
     * @param array $where
     * @return unknown|string
     * date:2017年1月5日
     * author: EK_熊
     */
    function allComment($page=1,$limit=10,$where=[]){
        $where['status'] = 1;
        $data = $this->where($where)->page($page,$limit)->field('id,uid,to_uid,content,created_time,parentid')->order('created_time DESC')->select();
        if (!$data) return $data;
        

        $data = $this->append_nickname($data);
        $tree = new Tree();
        $tree->init($data);
        $list = $tree->get_tree_array(0);
        return $list;
    }
    
    /**
     * 获取未读的消息数目
     * @param unknown $uid
     * @return unknown
     * date:2017年2月17日
     * author: EK_熊
     */
    function count_unread($uid){
        
        $where['to_uid'] = $uid;
        $where['read'] = 0;
        $ret = $this->where($where)->count();
        return $ret;
    }
    
    /**
     * 读消息，修改阅读状态
     * @param unknown $uid
     * @return boolean
     * date:2017年2月17日
     * author: EK_熊
     */
    function read_all_comment($uid){
        $where['to_uid'] = $uid;
        $where['read'] = 0;
        $isRead = $this->where($where)->find();
        if ($isRead) {
            
            $ret = $this->where($where)->setField('read',1);
        }
        
    }
    
    /**
     * 处理评论的昵称，包含来源uid和去向to_uid
     * @param unknown $data
     * @return unknown
     * date:2017年1月11日
     * author: EK_熊
     */
    function append_nickname($data){
        if (!$data) return null; 
        $uidBox = [];
        foreach ($data as $k=>$v) {
            $uidBox[] = $v['uid'];
            $touidBox[] = $v['to_uid'];
        }
        $uidList = array_merge(array_unique($uidBox),array_unique($touidBox));
        $request = D('Account','Service')->get_user_batch('ep_id',implode(',',$uidList),'ep_id,ep_name,nickname,avatar');
        foreach ($data as $k=>$v){
            for($i=0;$i<count($uidList);$i++) {
                $userInfo = $request[$v['uid']];
                $to_userInfo = $request['userInfo'][$v['to_uid']];
                if(in_array($v['uid'],$uidList))  {
                    $data[$k]['nickname'] = $userInfo['nickname'] ? $userInfo['nickname'] : $userInfo['ep_name'];
                    $data[$k]['avatar'] = $userInfo['avatar'];
                }
                if(in_array($v['to_uid'],$uidList))  {
                    $data[$k]['to_nickname'] = $to_userInfo['nickname'] ? $to_userInfo['nickname'] : $to_userInfo['ep_name'];
                    $data[$k]['to_avatar'] = $to_userInfo['avatar'];
                }
            }
        
        }
        return $data;
    }
    
    
    
    
}